package data.model;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

/**
 * Entwickler
 * 
 * @author: markus
 */
public class Developer extends AbstractModel implements Serializable {
	private static final long serialVersionUID = 5412222283479780707L;

	private final String id;
	private final Map<Project, ProjectDeveloper> projects;

	/**
	 * @param id
	 * @throws IllegalArgumentException
	 *             falls id null
	 */
	Developer(String id) throws IllegalArgumentException {
		super();
		if (id == null) {
			throw new IllegalArgumentException("id must not be null");
		}
		this.id = id;
		this.projects = new LinkedHashMap<Project, ProjectDeveloper>();
	}

	/**
	 * @return die id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @return die Prozesse
	 */
	Set<Process> getProcesses() {
		Set<Process> processes = new LinkedHashSet<Process>();
		for (ProjectDeveloper projectDeveloper : projects.values()) {
			processes.addAll(projectDeveloper.getProcesses());
		}
		return processes;
	}

	void addProcess(Process process) {
		projects.get(process.getProject()).addProcess(process);
		getProjectDeveloper(process.getProject()).getEstimatedEffort()
				.addPropertyChangeListener(process);
	}

	ProjectDeveloper getProjectDeveloper(Project project) {
		return projects.get(project);
	}

	public Integer getFirstWeekNumberWithoutData(Project project) {
		return projects.get(project).getFirstWeekNumberWithoutData();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Developer other = (Developer) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	public void addProject(ProjectDeveloper projectDeveloper) {
		assert (!projects.containsValue(projectDeveloper));
		projects.put(projectDeveloper.getProject(), projectDeveloper);
	}

}
